package com.cloudsigns.factorymanager2.project.model;

import android.text.TextUtils;
import android.util.Log;

import com.cloudsigns.factorymanager2.MyApplicaton;
import com.cloudsigns.factorymanager2.cusenum.CommonEnum;
import com.cloudsigns.factorymanager2.engineering.utils.EngineeringUtil;
import com.cloudsigns.factorymanager2.produce.bean.ProduceProduceOrderBean;
import com.cloudsigns.factorymanager2.produce.bean.Produce_ProductBean;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;
import com.cloudsigns.factorymanager2.project.utils.ProjectUtil;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class GetProduceOrderDetailModel implements IGetProduceOrderDetailModel {

    @Override
    public void getProduceOrderDetail(final String orderId, final OnProduceOrderDetailListener produceOrderDetailListener) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                HttpClient client = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost(ProjectUtil.PRODUCT_ORDER_DETAIL);
                HttpResponse response;
                try {
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("orderId", orderId);
                    Log.e("GetProduceOrderDetail", "jsonObject=" + jsonObject.toString());

                    StringEntity se = new StringEntity(jsonObject.toString(), HTTP.UTF_8);
                    httpPost.addHeader(HTTP.CONTENT_TYPE, ConstantUtil.CONTENT_TYPE);
                    httpPost.addHeader("token", MyApplicaton.getApplication().getToken());
                    httpPost.setEntity(se);
                    response = client.execute(httpPost);
                    String result = EntityUtils.toString(response.getEntity(), "UTF-8").trim();
                    JSONObject json = new JSONObject(result);
                    String code = json.getString("code");
                    Log.e("GetProduceOrderDetail", "result=" + result);
                    Log.e("GetProduceOrderDetail", "code=" + code);

                    if (null != produceOrderDetailListener) {
                        if (code.equals(EngineeringUtil.HTTP_SUCCESSED_COD)) {
                            JSONObject dataJsonObj = new JSONObject(json.getString("data"));
                            ProduceProduceOrderBean orderBean = new ProduceProduceOrderBean();


                            String completeDateStr = dataJsonObj.getString("completeDate");
                            if (!TextUtils.isEmpty(completeDateStr) && !completeDateStr.equals("null")) {
                                orderBean.setCompleteDate(Long.parseLong(completeDateStr));
                            }

                            String createDateStr = dataJsonObj.getString("createDate");
                            if (!TextUtils.isEmpty(createDateStr) && !createDateStr.equals("null")) {
                                orderBean.setCreateDate(Long.parseLong(createDateStr));
                            }

                            orderBean.setCreateOrderAccountId(dataJsonObj.getString("createOrderAccountId"));
                            orderBean.setCreateOrderAccountName(dataJsonObj.getString("createOrderAccountName"));


                            orderBean.setEngineeringName(dataJsonObj.getString("engineeringName"));

                            String isHurry = dataJsonObj.getString("isHurry");
                            if (!TextUtils.isEmpty(isHurry)) {
                                if (isHurry.equals("HURRY")) {
                                    orderBean.setIsHurry(CommonEnum.ProduceOrderIsHurry.HURRY);
                                } else if (isHurry.equals("NOT_HURRY")) {
                                    orderBean.setIsHurry(CommonEnum.ProduceOrderIsHurry.NOT_HURRY);
                                }
                            }

                            String level = dataJsonObj.getString("level");
                            if (!TextUtils.isEmpty(level)) {
                                if (level.equals("VERY_IMPORTANT")) {
                                    orderBean.setLevel(CommonEnum.ProduceOrderLevel.VERY_IMPORTANT);
                                } else if (level.equals("MORE_IMPORTANT")) {
                                    orderBean.setLevel(CommonEnum.ProduceOrderLevel.MORE_IMPORTANT);
                                } else if (level.equals("GENERALLY_IMPORTANT")) {
                                    orderBean.setLevel(CommonEnum.ProduceOrderLevel.GENERALLY_IMPORTANT);
                                } else if (level.equals("NORMAL")) {
                                    orderBean.setLevel(CommonEnum.ProduceOrderLevel.NORMAL);
                                }
                            }

                            orderBean.setOrderId(dataJsonObj.getString("orderId"));

                            String productListJsonArrStr = dataJsonObj.getString("productList");
                            if (null != productListJsonArrStr && !productListJsonArrStr.equals("null") && !TextUtils.isEmpty(productListJsonArrStr)) {
                                JSONArray productListJsonArr = new JSONArray(productListJsonArrStr);
                                List<Produce_ProductBean> productBeenLs = new ArrayList<Produce_ProductBean>();
                                for (int i = 0; i < productListJsonArr.length(); i++) {
                                    Produce_ProductBean productBean = new Produce_ProductBean();
                                    JSONObject productListJsonObj = (JSONObject) productListJsonArr.get(i);
                                    String amountStr = productListJsonObj.getString("amount");
                                    if (null != amountStr && !TextUtils.isEmpty(amountStr) && !"null".equals(amountStr)) {
                                        productBean.setAmount(Integer.parseInt(amountStr));
                                    }

                                    String beginDateStr=productListJsonObj.getString("beginDate");
                                    if (!TextUtils.isEmpty(beginDateStr)&&!beginDateStr.equals("null")){
                                        productBean.setBeginDate(Long.parseLong(beginDateStr));
                                    }

                                    productBean.setEngineeringName(productListJsonObj.getString("engineeringName"));

                                    String finishDateStr=productListJsonObj.getString("finishDate");
                                    if (!TextUtils.isEmpty(finishDateStr)&&!finishDateStr.equals("null")) {
                                        productBean.setFinishDate(Long.parseLong(finishDateStr));
                                    }

                                    String level1 = dataJsonObj.getString("level");
                                    if (!TextUtils.isEmpty(level1)) {
                                        if (level1.equals("VERY_IMPORTANT")) {
                                            orderBean.setLevel(CommonEnum.ProduceOrderLevel.VERY_IMPORTANT);
                                        } else if (level1.equals("MORE_IMPORTANT")) {
                                            orderBean.setLevel(CommonEnum.ProduceOrderLevel.MORE_IMPORTANT);
                                        } else if (level1.equals("GENERALLY_IMPORTANT")) {
                                            orderBean.setLevel(CommonEnum.ProduceOrderLevel.GENERALLY_IMPORTANT);
                                        } else if (level1.equals("NORMAL")) {
                                            orderBean.setLevel(CommonEnum.ProduceOrderLevel.NORMAL);
                                        }
                                    }
                                    productBean.setOrderId(productListJsonObj.getString("orderId"));

                                    String picUrlJsonArrStr = productListJsonObj.getString("picUrl");
                                    if (null != picUrlJsonArrStr && !picUrlJsonArrStr.equals("null") && !TextUtils.isEmpty(picUrlJsonArrStr)) {
                                        JSONArray picUrlJsonArr = new JSONArray(picUrlJsonArrStr);
                                        ArrayList<String> picUrlLs = new ArrayList<String>();
                                        for (int j = 0; j < picUrlJsonArr.length(); j++) {
                                            picUrlLs.add(picUrlJsonArr.getString(j));
                                        }
                                        productBean.setPicUrl(picUrlLs);
                                    }

                                    productBean.setProductId(productListJsonObj.getString("productId"));
                                    productBean.setProductName(productListJsonObj.getString("productName"));
                                    productBean.setProjectName(productListJsonObj.getString("projectName"));
                                    productBean.setShapeMap(productListJsonObj.getBoolean("shapeMap"));


                                    JSONObject shapeTypeJsonObj = productListJsonObj.getJSONObject("shapeType");
                                    if (null != shapeTypeJsonObj) {
                                        productBean.getShapeType().setBorder(shapeTypeJsonObj.getInt("border"));
                                        productBean.getShapeType().setBorderBottom(shapeTypeJsonObj.getInt("borderBottom"));
                                        productBean.getShapeType().setBorderLeft(shapeTypeJsonObj.getInt("borderLeft"));
                                        productBean.getShapeType().setBorderRight(shapeTypeJsonObj.getInt("borderRight"));
                                        productBean.getShapeType().setBorderTop(shapeTypeJsonObj.getInt("borderTop"));
                                        productBean.getShapeType().setDesc(shapeTypeJsonObj.getString("desc"));
                                        productBean.getShapeType().setDiameter(shapeTypeJsonObj.getInt("diameter"));
                                        productBean.getShapeType().setHeight(shapeTypeJsonObj.getInt("height"));
                                        productBean.getShapeType().setThick(shapeTypeJsonObj.getInt("thick"));
                                        String typeStr = shapeTypeJsonObj.getString("type");
                                        if (!TextUtils.isEmpty(typeStr)) {
                                            if (typeStr.equals("ROUND")) {
                                                productBean.getShapeType().setType( CommonEnum.ProductShapeType.ROUND);
                                            } else if (typeStr.equals("RECTANGLE")) {
                                                productBean.getShapeType().setType(CommonEnum.ProductShapeType.RECTANGLE);
                                            } else if (typeStr.equals("SPECIALSHAPE")) {
                                                productBean.getShapeType().setType( CommonEnum.ProductShapeType.SPECIALSHAPE);
                                            }
                                        }
                                        productBean.getShapeType().setWidth(shapeTypeJsonObj.getInt("width"));
                                    }

                                    productBean.setUnit(productListJsonObj.getString("unit"));

                                    productBeenLs.add(productBean);
                                }
                                orderBean.setProductList(productBeenLs);
                            }


                            orderBean.setProjectName(dataJsonObj.getString("projectName"));
                            orderBean.setReceiveAccountId(dataJsonObj.getString("receiveAccountId"));
                            orderBean.setReceiveAccountName(dataJsonObj.getString("receiveAccountName"));
                            String receiveOrderTimeStr = dataJsonObj.getString("receiveOrderTime");
                            if (!TextUtils.isEmpty(receiveOrderTimeStr) && !receiveOrderTimeStr.equals("null")) {
                                orderBean.setReceiveOrderTime(Long.parseLong(receiveOrderTimeStr));
                            }
                            String sendOrderTimeStr = dataJsonObj.getString("sendOrderTime");
                            if (!TextUtils.isEmpty(sendOrderTimeStr) && !sendOrderTimeStr.equals("null")) {
                                orderBean.setSendOrderTime(Long.parseLong(sendOrderTimeStr));
                            }
                            String statusStr = dataJsonObj.getString("status");
                            if (!TextUtils.isEmpty(statusStr)) {
                                if (statusStr.equals("CREATED")) {
                                    orderBean.setStatus(CommonEnum.ProduceOrderStatus.CREATED);
                                } else if (statusStr.equals("SEND")) {
                                    orderBean.setStatus(CommonEnum.ProduceOrderStatus.SEND);
                                } else if (statusStr.equals("RECEIVED")) {
                                    orderBean.setStatus(CommonEnum.ProduceOrderStatus.RECEIVED);
                                } else if (statusStr.equals("RETURNED")) {
                                    orderBean.setStatus(CommonEnum.ProduceOrderStatus.RETURNED);
                                } else if (statusStr.equals("COMPLETED")) {
                                    orderBean.setStatus(CommonEnum.ProduceOrderStatus.COMPLETED);
                                }
                            }

                            orderBean.setType(dataJsonObj.getString("type"));


                            produceOrderDetailListener.successed(orderBean);

                        } else {
                            String error_str = code;
                            error_str += "," + ConstantUtil.getErrorCode(code);
                            if (!json.isNull("error_str")) {
                                error_str += "," + json.getString("error_str");
                            }
                            produceOrderDetailListener.failed(error_str);
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {

                }
            }
        }).start();
    }

    public interface OnProduceOrderDetailListener {
        void successed(ProduceProduceOrderBean produceOrderBean);

        void failed(String desc);
    }
}
